//
//  banlirenViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/7.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class banlirenViewController: UIViewController {
    var tableView: UITableView!
    
    var ButtonTag:NSInteger!
    
    var xiangmu:String!
    var zhiwei:String!
    
    
    var json:String!
    
    var banlirenArray = [banlirenModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "办理人"
        self.view.backgroundColor = UIColor.white
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), style: .plain )
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorInset = UIEdgeInsets.zero
        //        tableView.separatorStyle = .none
        tableView.backgroundColor = bgGrayColor
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        //tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)//去除表格上放多余的空隙
        //tableView.tableHeaderView = UIView.init()
        tableView.tableFooterView = UIView.init()
        self.view.addSubview(tableView)
        
        
        self.loadData()
        
    }
    
    
    func loadData(){
        
        
        let manager = UserDefaults()
        
        
        let token: String? = manager.string(forKey:"tokens")  //根据关键字取值
        
        print("1111===\(token!)")
        
        
        
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/organization/list"
        
        if self.zhiwei == "办理人" {
            self.json = "{\"target\":\"\("assignee")\",\"token\":\"\(token!)\"}"
        }else if self.zhiwei == "审批人"{
            self.json = "{\"target\":\"\("shengpiAssignee")\",\"token\":\"\(token!)\"}"
        }else if self.zhiwei == "推送人"{
            self.json = "{\"target\":\"\("pushAssignee")\",\"token\":\"\(token!)\"}"
        }else{
            self.json = "{\"target\":\"\("questionAssignee")\",\"token\":\"\(token!)\"}"
        }
        
        
     

        
        
        let url = URL(string: urlString)!
        let jsonData = self.json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("2222222222json====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                
                print("2222222222json====\(json["message"])")
                let array = json["resultData"].array

                
                for dataDic in  array! {

                    let model1 = banlirenModel  ()
                    model1.id = dataDic["id"].string

                    model1.isParent = dataDic["isParent"].string
                    model1.name = dataDic["name"].string

                    model1.pId = dataDic["pId"].string
                    
                    model1.type = dataDic["type"].string

                    self.banlirenArray.append(model1)


                    print("NextListArray====\(self.banlirenArray.count)")

                }
                
                self.tableView.reloadData()
                
            }else{
                
                print("数据请求失败")
            }
            
            
            
            
        }
        
    }
    
    
    
    
}
extension banlirenViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.banlirenArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let banliren:banlirenModel = self.banlirenArray[indexPath.row]
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            
            cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = banliren.name
        cell?.textLabel?.font = UIFont.systemFont(ofSize:12)
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let banliren:banlirenModel = self.banlirenArray[indexPath.row]

        
        
            
            //实例化一个将要跳转的viewController
            let moreBanlirenVc = moreBanlirenViewController()
            moreBanlirenVc.parentID = banliren.id
            moreBanlirenVc.buttonTag = self.ButtonTag
        
            moreBanlirenVc.xiangmu = self.xiangmu!
        
            moreBanlirenVc.zhiwei = self.zhiwei
            self.navigationController?.pushViewController(moreBanlirenVc , animated: true)

        
        
        
    }
    
}
